# Figure 7

library(tidyverse)
library(grid)

DATA         <- read.csv(file.choose(), header=TRUE)

DATA$PCT     <- DATA$PE.SERIOUS / DATA$N

DATA$EVENT   <- factor(DATA$EVENT, levels=DATA$EVENT[1:11])

EVENT.AN     <- as.numeric(DATA$EVENT)
TEXT.SERIOUS <- textGrob(expression(bold("Serious")), gp=gpar(fontsize=21))
TEXT.CLOWN   <- textGrob(expression(bold("Clown"  )), gp=gpar(fontsize=21))

theme.z      <- theme(
        axis.text.x        = element_blank(),
        axis.text.x.top    = element_blank(),
        axis.text.y        = element_text(size=15 , color="black", margin=margin(t=10,r=10,b=10,l=10)),
        axis.text.y.right  = element_text(size=15 , color="black", margin=margin(t=10,r=10,b=10,l=10)),
        axis.ticks.x       = element_blank(),
        axis.ticks.y       = element_blank(),
        axis.title.x       = element_text(size=15 , color="black", margin=margin(t=10,r=10,b=10,l=10)),
        axis.title.y       = element_blank(),
        legend.position    = "none",
        panel.background   = element_rect(size=0.5, color="black", fill="gray90", linetype="solid"),
        panel.border       = element_rect(size=1.2, color="black", fill=NA      , linetype="solid"),
        panel.grid.major.x = element_blank(), 
        panel.grid.major.y = element_blank(),
        panel.grid.minor.x = element_blank(), 
        panel.grid.minor.y = element_blank(),
        plot.background    = element_rect(fill="white"),
        plot.caption       = element_text(size=12 , hjust=0  , margin=margin(t=10)),
        plot.margin        = unit(c(t=50,r=5,b=5,l=5),"pt"),
        plot.subtitle      = element_text(size=15 , hjust=0.5),
        plot.title         = element_text(size=20 , hjust=0.5, margin=margin(t=0,b=35), face="bold"),
        strip.background   = element_rect(color="black", fill="black"),
        strip.text.x       = element_text(size=18, color="white", face="bold", margin=margin(t=7.5,b=7.5)))

ggplot(data=DATA, aes(x=rev(EVENT.AN), y=100*PCT)) +
  coord_flip(clip="off") +
  scale_fill_manual(values=c("purple")) +
  geom_col(color="black", size=0.85, width=0.7, fill="purple") +
  geom_segment(x=rev(EVENT.AN), xend=rev(EVENT.AN), y=0, yend=100*DATA$PE.CLOWN/DATA$N) +
  geom_point(y=100*DATA$PE.CLOWN/DATA$N, x=rev(EVENT.AN), shape=21, size=4.5, color="black", fill="orange", stroke=1.25) +
  geom_text(aes(x=rev(EVENT.AN), y=c(100*PCT), label=scales::percent(abs(PCT), accuracy=1L))       , hjust=1, size=c(rep_len(5,11)), nudge_y=c(rep_len(-3,11))) +
  geom_text(aes(x=rev(EVENT.AN), y=45        , label=scales::percent(abs(PE.CLOWN/N), accuracy=1L)), hjust=1, size=c(rep_len(5,11))) +
    scale_x_continuous(breaks=1:11, labels=rev(DATA$EVENT[1:11]), sec.axis=sec_axis(~.,breaks=1:11, labels=rev(DATA$DATE[1:11]))) +
  scale_y_continuous(limits=c(-45,45)) +
annotation_custom(TEXT.SERIOUS, xmin=12.75, xmax=12.75, ymin=-22.5, ymax=-22.5) + 
  annotation_custom(TEXT.CLOWN  , xmin=12.75, xmax=12.75, ymin= 22.5, ymax= 22.5) +
  labs(y="Percentage of all articles") +
  theme.z

ggsave(file="Figure 7.wmf", width=8, height=5.5)
getwd()

